const { ipcRenderer, remote } = require("electron");
const path = require('path')
const fs = require('fs');

function $ (id) {
    return document.getElementById(id)
}

function getE (className) {
    return document.getElementsByClassName(className)
}

$('open').onclick = () => {
    ipcRenderer.send('new-file')
}

$('iconfont').onclick = () => {
    var inPut = $('input');
    var search = $('search');
    var isSure = Number(inPut.style.width.replace(/[^\d]/g, ''))
    console.log(inPut.value === '')
    if (isSure < 100) {
        var width = 0;
        var timer = setInterval(() => {
            width += 10;
            inPut.style.width = width + 'px';
            search.style.width = width + 'px';
            if (width >= 150) {
                clearInterval(timer)
            }
        }, 15)
    } else {
        if (inPut.value === '') {     
            var width = 150;
            var timer = setInterval(() => {
                width -= 10;
                inPut.style.width = width + 'px';
                search.style.width = width + 'px';
                if (width <= 0) {
                    clearInterval(timer)
                }
            }, 15)
        } else {
            console.log(__dirname)
            var path = __dirname.split('\\');
            var fileName = '';
            for (var i = 0, n = path.length; i < n-2; i++) {
                fileName += path[i] + '\\'
            }
            fileName += '\\files\\' + inPut.value + '.txt';
            console.log(fileName);
            var html = '';
            fs.access(fileName, (err) => {
                if (err) {
                    console.log('暂定')
                    $('content').style.display = 'none';
                    $('clear').style.display = 'block';          
                } else {
                    console.log('success')
                    html += `<div class="con-list">
                                <img class="con-list-left" src="../../image/file.png" alt="文件">
                                <p class="con-list-p">${inPut.value}</p>
                                <img class="con-list-mid" src="../../image/delete.png" alt="删除文件">
                                <img class="con-list-right" src="../../image/openFile.png" alt="打开文件">
                            </div>`                   
                    $('clear').style.display = 'none';  
                    $('content').style.display = 'block';
                    $('content').innerHTML = html;
                }
            })
        }      
    }    
}

$('content').onmousemove = (event) => {
    var list = getE('con-list')
    var deleteFile = getE('con-list-mid')
    var openFile = getE('con-list-right')
    var oP = getE('con-list-p')
    var calcY = Math.floor((event.pageY - 47) / 76)
    openFile[calcY].onclick = () => {
        ipcRenderer.send('open-file', calcY)
        remote.getGlobal('shareObject').calc = oP[calcY].innerHTML;
    }
    deleteFile[calcY].onclick = () => {
        var path = __dirname;
        path = path.split('\\');
        var filename = '';
        for (var i = 0, n = path.length; i < n-2; i++) {
            filename += path[i] + '\\'
        }
        filename += '\\files\\' + oP[calcY].innerHTML + '.txt'
        fs.unlink(filename, (error) => {
            if (error) {
                console.log(error)
            } else {
                $('content').removeChild(list[calcY])
            }
        })
    }
    if (calcY >= -0.5) {
        var timer = null;
        if (timer) {
            clearTimeout(timer)
        }
        timer = setTimeout(() => {
            for (var i = 0, n = list.length; i < n; i++) {
                list[i].style.backgroundColor = '#fff';
                list[i].style.borderLeft = 'none'
            }
            list[calcY].style.backgroundColor = '#e6e6e6';
            list[calcY].style.borderLeft = '5px solid #ec7259';
        }, 100)
    }
}

function update () {
    var filePath = __dirname.split('\\')
    var projectPath = '';
    for (var i = 0, n = filePath.length; i < n; i++) {
        if (i !== filePath.length - 2) {
            projectPath += filePath[i] + '\\'
        }
    }
    var arr = [];
    var str = null;
    var html = '';
    fs.readdir(projectPath, (err, files) => {
        if (err) {
            console.log('呕吼完蛋')
            console.log(err)
        } else {
            for (var i = 0, n = files.length; i < n; i++) {
                str = files[i].split('.')[0];
                arr[i] = str;
                html += `<div class="con-list">
                            <img class="con-list-left" src="../../image/file.png" alt="文件">
                            <p class="con-list-p">${arr[i]}</p>
                            <img class="con-list-mid" src="../../image/delete.png" alt="删除文件">
                            <img class="con-list-right" src="../../image/openFile.png" alt="打开文件">
                        </div>`
            }
            $('content').innerHTML = html
        }
    })
}

setInterval(() => {
    if (remote.getGlobal('shareObject').update) {
        update()
        remote.getGlobal('shareObject').update = false
    }
}, 100)

update()